package com.yanqu.road.server.cmd.activity.apprentice;

import com.yanqu.road.entity.activity.apprentice.ApprenticeDoOneProjectResult;
import com.yanqu.road.entity.activity.apprentice.ApprenticeDoProjectResult;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ApprenticeProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeActivity;
import com.yanqu.road.server.manager.activity.apprentice.CrossApprenticeMgr;
import com.yanqu.road.server.manager.activity.apprentice.pb.ApprenticePb;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_APPRENTICE_DO_PROJECT, desc = "爱徒养成-执行项目")
public class CrossApprenticeDoProjectCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ApprenticeProto.ApprenticeDoProjectReqMsg reqMsg = ApprenticeProto.ApprenticeDoProjectReqMsg.parseFrom(packet.getBytes());
        ApprenticeProto.ApprenticeDoProjectRespMsg.Builder respMsg = ApprenticeProto.ApprenticeDoProjectRespMsg.newBuilder();
        respMsg.setLevel(reqMsg.getLevel());
        respMsg.setActivityId(reqMsg.getActivityId());
        CrossApprenticeActivity activity = CrossApprenticeMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_APPRENTICE_DO_PROJECT, respMsg));
            return;
        }
        try {
            ApprenticeDoProjectResult result = activity.doProjects(packet.getPlayerId(), reqMsg.getProjectIdsList(), reqMsg.getLevel());
            respMsg.setRet(0);
            respMsg.setUserData(ApprenticePb.buildUserData(result.getUserData(), activity.getCanSelectProjectNum(result.getUserData())));
            if (result.getExamData() != null) {
                respMsg.setExamResult(ApprenticePb.buildExamData(result.getExamData()));
            }
            respMsg.setUpgradeFlag(result.isUpgradeFlag());
            respMsg.setProgramResult(ApprenticePb.buildProgramResult(result.getProgramResult()));
            for (ApprenticeDoOneProjectResult projectResult : result.getOneProjectResults()) {
                respMsg.addProjectResults(ApprenticePb.buildProjectResult(projectResult));
            }
            respMsg.setActivityId(reqMsg.getActivityId());
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_APPRENTICE_DO_PROJECT_FROM_CROSS, respMsg));
    }

    @Override
    public void sendData() {

    }
}
